home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12284 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  43 lines

  1. Path: Kaos.deepcove.com!saffleck
  2. From: saffleck@deepcove.com (Sean Affleck)
  3. Newsgroups: comp.lang.c++
  4. Subject: Help! Newbie question ...
  5. Date: Tue, 19 Mar 96 06:33:29 GMT
  6. Organization: deepcove.com
  7. Message-ID: <4ilkfu$41q@Kaos.deepcove.com>
  8. NNTP-Posting-Host: b06m.deepcove.com
  9. X-Newsreader: News Xpress 2.0 Beta #0
  10. Keyword: returning objects statically declared in functions
  11.  
  12. Is this legal ? 
  13.  
  14. class A {...};
  15.  
  16. A f();
  17.  
  18. main() {
  19.   A a = f();
  20. }
  21.  
  22. A f()
  23. {
  24.    A fa;
  25.    return fa;
  26. }
  27.  
  28.         Can you create an object on the stack within a function and then return 
  29. the object ?  I know that objects created on the stack are deleted at block or
  30.  function exit, so is it ok to return them and use the value to initialize 
  31. another object?  
  32.         I realize that there are ways to get around this such as dynamically 
  33. allocating the object and returning a pointer or passing the function an 
  34. object which the function can initialize in whatever way it wants.  However, 
  35. I'd like to know if this can be done 'cus it seems more tidy since the 
  36. returned object is automaticly deleted on block exit and can therefore be 
  37. ignored if not needed.  I'd also like to know for my own personal interest. 
  38.                         
  39.                                         Thanks!
  40.  
  41.         Sean Affleck
  42.         nbennett@fraser.sfu.ca
  43.